package com.dhf.framework.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.dhf.framework.base.mybatis.core.page.Pagination;
import com.dhf.framework.common.BaseController;
import com.dhf.framework.system.entity.SysTableColumn;
import com.dhf.framework.system.entity.SysTableInfo;
import com.dhf.framework.system.service.CommonService;
import com.dhf.framework.system.service.SysTableColumnService;
import com.dhf.framework.system.service.SysTableInfoService;

 /**
 * </b>SysTableColumnController<br>表字段信息
 * @author my
 * @version:1.0
 */
@Controller
@RequestMapping("/system/sysTableColumn")
public class SysTableColumnController extends BaseController{
		private static Logger LOGGER = LoggerFactory.getLogger(SysTableColumnController.class);
	
  		@Autowired
  		private SysTableColumnService sysTableColumnService;
  		
  		@Autowired
  		private SysTableInfoService sysTableInfoService;
  		
  		
  		@Autowired
  		private CommonService commonService;

  		@Value("${tableSchema}")
  		private String tableSchema;
  		
  		/**
		  * 
		  * @Title: queryPageList
		  * @Description: 列表页面
		  * @param @param searchEntity
		  * @param @param pagination
		  * @return ModelAndView    返回类型
		  * @throws
		*/
		@RequestMapping(value = "/queryPageList")
		public ModelAndView queryPageList(SysTableColumn sysTableColumn, Pagination<SysTableColumn> pagination) {
			LOGGER.debug(SysTableColumnController.class.getName()+":开始查询分页数据！");
			ModelAndView mv = new ModelAndView( "system/sysTableColumn/sysTableColumnList" );
			pagination = this.sysTableColumnService.queryPageList( sysTableColumn, pagination );
			mv.addObject( "pagination", pagination );
			List<SysTableInfo> list=this.sysTableInfoService.query(null);
			mv.addObject("list", list);
			return mv;
		}
		
		
		/**
		 * 
		 * @Title: toMerge
		 * @Description: 新增或修改页面
		 * @param @param entity
		 * @return ModelAndView    返回类型
		 * @throws
		 */
		@RequestMapping(value = "/toMerge")
		public ModelAndView toMerge(SysTableColumn entity){
			ModelAndView mv = new ModelAndView("system/sysTableColumn/sysTableColumnMerge");
			if(entity.getId()!= null){
				SysTableColumn sysTableColumn =  this.sysTableColumnService.get( entity.getId() ) ;
				mv.addObject("sysTableColumn",sysTableColumn);
			}else{
				mv.addObject("sysTableColumn", new SysTableColumn());
			}
			List<SysTableInfo> list=this.sysTableInfoService.query(null);
			mv.addObject("list", list);
			return mv;
		}

		/**
		 * 
		* @Title: doMerge
		* @Description: 新增或修改数据 
		* @param @param request
		* @param @param entity
		* @return ModelAndView    返回类型
		* @throws
		 */
		@RequestMapping(value = "/doMerge")
		public ModelAndView doMerge(HttpServletRequest request, SysTableColumn entity,RedirectAttributes redirectAttributes) {
			SysTableInfo sysTableInfo=new SysTableInfo();
			sysTableInfo.setId(entity.getTableInfoId());
			List<SysTableInfo> list=this.sysTableInfoService.query(sysTableInfo);
			if(!list.isEmpty()){
				entity.setTableName(list.get(0).getTableName());
			}
			
			if (entity.getId()!=null) {
				LOGGER.debug(SysTableColumnController.class.getName()+":开始更新数据！");
				this.addMessage(redirectAttributes, "修改数据成功");
				entity.setModifyTime(new Date());
				this.sysTableColumnService.update( entity );
			} else {
				LOGGER.debug(SysTableColumnController.class.getName()+":开始新增数据！");
				entity.setCreateTime(new Date());
				this.sysTableColumnService.add( entity );
				this.addMessage(redirectAttributes, "新增据成功");
			}
			return new ModelAndView( "redirect:/system/sysTableColumn/queryPageList.do" );
		}

		/**
		 * 
		 * @Title: delete
		 * @Description:删除数据
		 * @param @param id
		 * @return ModelAndView    返回类型
		 * @throws
		 */
		@RequestMapping(value = "/delete")
		public ModelAndView delete(Long id,RedirectAttributes redirectAttributes) {
			LOGGER.debug(SysTableColumnController.class.getName()+":开始删除数据！");
	        this.sysTableColumnService.delete( id );
	        this.addMessage(redirectAttributes, "删除数据成功");
	        return new ModelAndView( "redirect:/system/sysTableColumn/queryPageList.do" );
		}
		
		
		/**
		 * 
		* @Description: 同步表字段数据
		* @param @param redirectAttributes
		* @param @return    设定文件
		* @return ModelAndView    返回类型
		 */
		@RequestMapping(value = "/sysTableColumn")
		public ModelAndView sysTableColumn(RedirectAttributes redirectAttributes) {
			LOGGER.debug(SysTableColumnController.class.getName()+":开始同步表字段数据！");
			List<Map<String, Object>> list = this.commonService.queryAllTables(tableSchema);
			for (Map<String, Object> map : list) {
				String tableName = MapUtils.getString(map, "table_name", "");
				SysTableInfo searchEntity = new SysTableInfo();
				searchEntity.setTableName(tableName);
				List<SysTableInfo> searchList = this.sysTableInfoService.query(searchEntity);
				if (StringUtils.isNotEmpty(tableName) && CollectionUtils.isNotEmpty(searchList)) {
					List<Map<String, Object>> columnList=this.commonService.queryTableFieldAttributes(tableSchema, tableName);
					for(Map<String, Object> columnMap :columnList){
						String columnName = MapUtils.getString(columnMap, "column_name", "");
						String columnComment = MapUtils.getString(columnMap, "column_comment", "");
						String dataType = MapUtils.getString(columnMap, "DATA_TYPE", "");
						String columnKey = MapUtils.getString(columnMap, "column_key", "");
						
						SysTableColumn searchColumn=new SysTableColumn();
						searchColumn.setTableName(tableName);
						searchColumn.setColumnName(columnName);
						List<SysTableColumn> columnNameList=this.sysTableColumnService.query(searchColumn);
						if(columnNameList.isEmpty()){
							SysTableColumn sysTableColumn=new SysTableColumn();
							sysTableColumn.setTableInfoId(searchList.get(0).getId());
							sysTableColumn.setTableName(searchList.get(0).getTableName());
							sysTableColumn.setColumnName(columnName);
							sysTableColumn.setColumnComment(columnComment);
							sysTableColumn.setColumnType(dataType);
							if("PRI".equals(columnKey)){
								sysTableColumn.setIsPrimary("1");
							}else{
								sysTableColumn.setIsPrimary("2");
							}
							sysTableColumn.setIsDisplay("1");
							sysTableColumn.setIsQuery("2");
							sysTableColumn.setIsAdd("1");
							sysTableColumn.setIsModify("1");
							sysTableColumn.setIsDetail("1");
							sysTableColumn.setCreateTime(new Date());
							this.sysTableColumnService.add(sysTableColumn);
						}else{
							LOGGER.error(tableName+":"+columnName+"已经存在");
						}
					}
				}
			}
			this.addMessage(redirectAttributes, "同步表字段数据成功");
	        return new ModelAndView( "redirect:/system/sysTableColumn/queryPageList.do" );
		}
}

